
/**
 * primitive Polyline 大量加载
 * 
 */
import Primitive from "../Primitive"

const DEFAULT_STYLE = {
  width: 2
}

export default class PolylinePrimitive extends Primitive {
  /**
   * @param {*} positions 
   * @param {*} options 
   */
  constructor(positions, options = {}) {
    super()
    this._positions = positions
    this._type = 'polyline_primitive'
    this._style = {
      ...DEFAULT_STYLE,
      ...options
    }
    this.delegate = new Cesium.Primitive({
      geometryInstances: new Cesium.GeometryInstance({
        geometry: {}
      })
    })
    this.mounted()
  }

  /**
   * 属性初始化
   */
  mounted() {
    this.positions = this._positions
     this.style = this._style
    !this.delegate.appearance && this.setAppearance({type: 'Color'})
  }

  /**
   * 设置材质
   * @returns 
   */
  setAppearance(MaterialOption, options = {}) {
    if (!MaterialOption) return

    let type = MaterialOption.type
    delete MaterialOption.type

    this.delegate.appearance = new Cesium.PolylineMaterialAppearance({
      material: Cesium.Material.fromType(type, {
        ...MaterialOption
      }),
      ...options
    })
  }

  /**
   * 
   * @param {*} style 
   * @returns 
   */

  setStyle(style) {
    if (!style) return
    Object.assign(this._style, { ...style })
    Object.assign(this.delegate.geometryInstances.geometry, {
      ...this._style
    })
  }

  get positions() {
    return this._positions
  }

  set positions(positions) {
    this._positions = positions
    this.delegate.geometryInstances.geometry = new Cesium.PolylineGeometry({
      positions: this._positions,
      width: 2
    })
    return this
  }
}
